from time import sleep
from selenium import webdriver
driver=webdriver.Firefox()
driver.get('https://www.baidu.com/index.php?tn=monline_3_dg')
sleep(2)
driver.find_element_by_link_text('新闻').click()
sleep(2)
# 获取句柄
hds = driver.window_handles
# 切换句柄
driver.switch_to.window(hds[1])
sleep(2)
driver.find_element_by_link_text('军事').click()
sleep(2)
# hds = driver.window_handles
# driver.switch_to.window(hds[-1])
driver.find_element_by_css_selector('#col_focus > div:nth-child(1) > div:nth-child(2) > ul:nth-child(1) > li:nth-child(1) > a:nth-child(1)').click()
# driver.find_element_by_link_text('新装，入列！').click()
sleep(2)
hds = driver.window_handles
driver.switch_to.window(hds[-1])
driver.close()
driver.switch_to.window(hds[1])
driver.close()
driver.switch_to.window(hds[0])
driver.find_element_by_id("kw").send_keys("selenium")
sleep(2)
driver.find_element_by_id("su").click()
sleep(2)
driver.quit()